﻿$(document).ready(function () {
    $("#C1Btndown").click(function () { byerinfoShow(1); return false; });
    $("#C2Btnup").click(function () { byerinfoShow(2); return false; });
    $("#C2Btndown").click(function () { byerinfoShow(3); return false; });
    $("#C3Btnup").click(function () { byerinfoShow(4); return false; });
    $("#C3Btndown").click(function () { byerinfoShow(5); return false; });
    $("#C4Btnup").click(function () { byerinfoShow(6); return false; });
    $("#C4Submit").click(function () { submitForm(); return false; });
    //$(".div_tip").corner("5px");
    $(".maintextcss").bind("focusin focusout", function () {
        $(this).parent().next(".div_tip").toggleClass("div_tip_current");
    });
    KE.show({
        id: "info",
        newlineTag: "br",
        resizeMode: 0,
        allowPreviewEmoticons: false,
        allowUpload: false,
        allowPreviewEmoticons: true,
        items: ['undo', 'redo', "|", 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', "|", 'selectall', 'fontname', 'fontsize', '|', 'textcolor', 'bold',
                'italic', 'removeformat', '|', 'emoticons'],
        afterChange: function (id) {
            if (KE.count(id) > 8000) {
                KE.$('wordcount').innerHTML = "您输入的字符已超过8000";
                $("#wordcount").css("color", "red");
            }
            else {
                KE.$('wordcount').innerHTML = 8000 - KE.count(id) + "/8000";
                $("#wordcount").css("color", "");
            }
        }
    });
    $("#pricemax").bind("keypress", function (event) {
        var keynum, keychar, numcheck
        var result = false;
        var defaultVal = "0.0";
        if ($(this).val()) {
            defaultVal = $(this).val();
        }
        var money = /^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/;
        if (window.event) // IE
        {
            keynum = event.keyCode;
        }
        else if (e.which) // Netscape/Firefox/Opera
        {
            keynum = event.which;
        }
        keychar = String.fromCharCode(keynum);
        numcheck = /\d/;
        if (money.test(defaultVal + keychar)) {
            result = true;
        }
        else {
            if (defaultVal.indexOf(".") > 0) {
                if (defaultVal.split(".").length >= 2) {
                    return false;
                }
            }
            else {
                if (keychar == ".") {
                    result = true;
                }
                else {
                    return false;
                }
            }
        }
        if (numcheck.test(keychar)) {
            result = true;
        }
        else {
            if (keychar == ".") {
                result = true;
            }
            else {
                result = false;
            }
        }
        return result;
    });
});
$(document).ready(function () {
    $("#mainpage").hover(function () { $(this).addClass("mainActive"); }, function () { $(this).removeClass("mainActive"); });
    $("#mybuy").hover(function () { $(this).addClass("mybuylinkactive"); }, function () { $(this).removeClass("mybuylinkactive"); });
    $("#allbuy").hover(function () { $(this).addClass("allbuylinkactive"); }, function () { $(this).removeClass("allbuylinkactive"); });
    $("#cit1").change(function () { Bindlist(this, 2); getselectCit(1); });
    $("#cit2").change(function () { Bindlist(this, 3); getselectCit(2); });
    $("#cit3").change(function () { Bindlist(this, 4); getselectCit(3); });
    $("#cit4").change(function () { Bindlist(this, 5); getselectCit(4); });
    $("#cit5").change(function () { Bindlist(this, 6); getselectCit(5); });
    $("#cit6").change(function () { getselectCit(6); });

});
function listprev() {
    $(".prev").unbind();
    $(".prev").removeClass("prevok prevhover prevactive");
    $("#citul").animate({ left: '+=210px' }, "fast", function () {
        citlistmove();
    });
}
function listnext() {
    $(".next").unbind();
    $(".next").removeClass("nextok nexthover nextactive");
    $("#citul").animate({ left: '-=210px' }, "fast", function () {
        citlistmove();
    });
}

function citlistmove() {
    var childposition = $("#citul").position().left;
    //上一级按钮
    if (childposition != 0) {
        if (!$(".prev").hasClass("prevok")) {
            $(".prev").addClass("prevok");
            $(".prev").bind({
                mouseover: function () {
                    $(this).toggleClass("prevhover");
                },
                mouseout: function () {
                    $(this).toggleClass("prevhover");
                },
                mousedown: function () {
                    $(this).toggleClass("prevactive");
                },
                mouseup: function () {
                    $(this).toggleClass("prevactive");
                },
                click: function () {
                    listprev();
                }
            });
        }
    }
    else {
        $(".prev").unbind();
        $(".prev").removeClass("prevok prevhover prevactive");
    }
    //下一级按钮
    if (childposition <= listcount) {
        $(".next").unbind();
        $(".next").removeClass("nextok nexthover nextactive");
    }
    else {
        if (!$(".next").hasClass("nextok")) {
            $(".next").addClass("nextok");
            $(".next").bind({
                mouseover: function () {
                    $(this).toggleClass("nexthover");
                },
                mouseout: function () {
                    $(this).toggleClass("nexthover");
                },
                mousedown: function () {
                    $(this).toggleClass("nextactive");
                },
                mouseup: function () {
                    $(this).toggleClass("nextactive");
                },
                click: function () {
                    listnext();
                }
            });
        }
    }
}


//限制输入文字与其他字符
function noNumbers(e,obj) {
    var keynum, keychar, numcheck
    var result = false;
    var defaultVal = "0.0";
    if ($(obj).val()) {
        defaultVal = $(obj).val();
    }
    var money = /^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/;
    
    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    keychar = String.fromCharCode(keynum);
    numcheck = /\d/;

    if (money.test(defaultVal + keychar)) {
        result = true;
    }
    else 
    {
        if (defaultVal.indexOf(".") > 0) {
            if (defaultVal.split(".").length >= 2) {
                return false;
            }
        }
        else {
            if (keychar == ".") {
                result = true;
            }
            else {
                return false;
            }
        }
    }
    if (numcheck.test(keychar)) {
        result = true;
    }
    else 
    {
        if (keychar == ".") {
            result = true;
        }
        else {
            result = false;
        }
    }
    return result;
}
//上传图片
$(document).ready(function () {
    new AjaxUpload("#upload", {
        action: "/UploadHandler.ashx",
        name: "myfile",
        autoSubmit: true,
        onChange: function (file, extension) {
            if (!(extension == "png" || extension == "jpg" || extension == "jpeg" || extension == "gif")) {
                showerrdialog("请选择图片文件!");
                return false;
            }
            else {
                $("#showimg").hide();
                $("#showimg").attr("src", "");
                $("#imgParent").css("background", "#FFFFFF url('/Webimg/loading.gif') no-repeat 50% 50%");
            }
        },
        onSubmit: function () {
            this.disable();
        },
        onComplete: function (file, response) {
            if (response == "outofsize") {
                showerrdialog("图片文件大于500KB，无法上传!");
            }
            else {
                var imgsrc = "/uploadTemp/" + response;
                var $img = $("#showimg");
                $img.loadthumb({
                    "src": imgsrc,
                    "imgId": "showimg",
                    "parentId": "imgParent"
                });
            }
            this.enable();
            $("#imgParent").css("background", "#FFFFFF");
        }
    });
    $("#cancal").click(function () {
        if ($("#showimg").attr("src") != "") {
            $("#showimg").attr("src", "");
            $("#showimg").hide();
        }
        return false;
    });
});
function Bindlist(obj, index) {
    for (var i = index; i <= 6; i++) {
        $("#cit" + i).css("display", "none");
        $("#cit" + i).empty();
    }
    var svalue = $(obj).val();
	if(svalue == 0)
	{
	    return;
	}
	$("#cit" + index).attr("length", "0");
	$.ajax({
	    type: "post",
	    url: "/AjaxCommand/BindCit.ashx",
	    data: { type: "cit", cid: svalue },
	    beforeSend: function (jqXHR) {
	        asyncbox.open({
	            id: 'getcatlist',
	            title: '',
	            width: 330,
	            height: 115,
	            html: "<div class='dialogcss'><p><img src='/images/bigload.gif' style='vertical-align:middle;' /><label class='loadcss'><strong>&nbsp加载中...</strong></label></p></div>"
	        });
	        $(".asyncbox_close").remove();
	    },
	    success: function (data, textStatus, jqXHR) {
	        if (data == "") {
	            $("#cit" + index).css("display", "none");
	        }
	        else {
	            $("#cit" + index).append(data);
	            $("#cit" + index).show();
	        }
	        if (index == 5) {
	            listcount = -210;
	        }
	        else if (index == 6) {
	            listcount = -420;
	        }
	        else {
	            listcount = 0;
	        }
	        citlistmove();
	        $.close("getcatlist");
	    },
	    error: function (jqXHR, textStatus, errorThrown) {
	        $.close("getcatlist");
	        alert("链接已超时，重新刷新页面!");
	    }
	});
}
function placeChoose() {
    var p = $("input[name='place']:checked").val();
    if (p == 1) {
        $("#sheng").attr("disabled", false);
        $("#shi").attr("disabled", false);
    }
    else {
        $("#sheng").attr("disabled", true);
        $("#shi").attr("disabled", true);
    }
}
//***************************全国省市联动下拉框*******************************//
$(document).ready(function () { init(); sel(); });
function init() {
    //初始化 省份下拉列表里面列出所有可选省份，地区下拉列表清空，市县下拉列表清空
    $("#sheng").empty();
    $("<option value=\"0\">请选择省/市</option>").appendTo($("#sheng"));
    $.each(GP, function (n, ele) {
        $("<option value=" + ele + ">" + ele + "</option>").appendTo($("#sheng"));
    });
    //清空地区下拉列表
    $("#shi").empty();
    $("<option value=\"0\">请选择市/地区</option>").appendTo($("#shi"));
    if ($("#zhen").length > 0)
        $("#zhen").remove();
}
function sel() {
    $("#sheng").change(
		function () {
			$("#shi").empty();	
			$.each(GC1[$("#sheng option:selected").html()], function (n, ele) {
				$("<option value=" + ele + ">" + ele + "</option>").appendTo($("#shi"));
            });
			if ($("#zhen").length > 0)
				$("#zhen").remove();
		}
	);
}
////***************************全国省市联动下拉框END*******************************//
function byerinfoShow(obj) {
    switch (obj) {
        case 1:
            if ($("#cit1").val() == null) {
                showerrdialog("请选择商品类别！");
                return;
            }
            $("#Content1").hide();
            $("#Content2").show();
            $("#buydirection > span").eq(1).css("background", "url(/Webimg/buydirection.png) no-repeat -84px 0px")
            $("#buydirection > span").eq(2).addClass("current");
            if (!$("#buydirection > span").eq(3).hasClass("linecurrent")) {
                $("#buydirection > span").eq(3).removeClass("line");
                $("#buydirection > span").eq(3).addClass("linecurrent");
            }
            return;
        case 2:
            $("#Content2").hide();
            $("#Content1").show();
            return;
        case 3:
            if ($("#title").val() == "") {
                showerrdialog("请输入主题！");
                $("#title").focus();
            }
            else if ($("#name").val() == "") {
                showerrdialog("请输入名称！");
                $("#name").focus();
            }
            else if ($("#pricemax").val() == "") {
                showerrdialog("请输入期望价格！");
                $("#pricemax").focus();
            }
            else if (isNaN($("#pricemax").val())) {
                showerrdialog("期望价格请输入数字！");
                $("#pricemax").focus();
            }
            else if ($("#pricemax").val() - 0 <= 0) {
                showerrdialog("期望价格必须大于0！");
                $("#pricemax").focus();
            }
            else if ($("#goodscount").val() == "") {
                showerrdialog("请输入购买数量！");
                $("#goodscount").focus();
            }
            else {
                $("#Content2").hide();
                $("#Content3").show();
                $("#buydirection > span").eq(3).css("background", "url(/Webimg/buydirection.png) no-repeat -84px 0px")
                $("#buydirection > span").eq(4).addClass("current");
                if (!$("#buydirection > span").eq(5).hasClass("linecurrent")) {
                    $("#buydirection > span").eq(5).removeClass("line");
                    $("#buydirection > span").eq(5).addClass("linecurrent");
                }
            }
            return;
        case 4:
            $("#Content3").hide();
            $("#Content2").show();
            return;
        case 5:
            $("#Content3").hide();
            $("#Content4").show();
            $("#buydirection > span").eq(5).css("background", "url(/Webimg/buydirection.png) no-repeat -84px 0px")
            $("#buydirection > span").eq(6).addClass("current");
            return;
        case 6:
            $("#Content4").hide();
            $("#Content3").show();
            return;
        default:
            return;
    }
}

function submitForm() {
    var cid, pcid, title, owner, pricem, pricex, count, imgpath, sheng, shi, infomation, pname, cidtree, cidtextTree, keyprop, overtime,infortext;
    sheng = "";
    shi = "";
    keyprop = "";
    pcid = $("#cit1").val();
    overtime = $("#overtime").val();
    /****************提交数据准备**************/
    for (var i = 6; i > 0; i--) {
        if ($("#cit" + i).val() && ($("#cit" + i).val() != 0)) {
            cid = $("#cit" + i).val()
            break;
        }
    }
    for (var i = 6; i > 0; i--) {
        if ($("#cit" + i).val() == 0) {
            keyprop = $("#cit" + i).find("option:selected").text();
            break;
        }
    }
    if ($("#cit1").val() && ($("#cit1").val() != 0)) {
        cidtree = $("#cit1").val();
        cidtextTree = $("#cit1").find("option:selected").text();
        if ($("#cit2").val() && ($("#cit2").val() != 0)) {
            cidtree += "," + $("#cit2").val();
            cidtextTree += "," + $("#cit2").find("option:selected").text();
            if ($("#cit3").val() && ($("#cit3").val() != 0)) {
                cidtree += "," + $("#cit3").val();
                cidtextTree += "," + $("#cit3").find("option:selected").text();
                if ($("#cit4").val() && ($("#cit4").val() != 0)) {
                    cidtree += "," + $("#cit4").val();
                    cidtextTree += "," + $("#cit4").find("option:selected").text();
                    if ($("#cit5").val() && ($("#cit5").val() != 0)) {
                        cidtree += "," + $("#cit5").val();
                        cidtextTree += "," + $("#cit5").find("option:selected").text();
                        if ($("#cit6").val() && ($("#cit6").val() != 0)) {
                            cidtree += "," + $("#cit6").val();
                            cidtextTree += "," + $("#cit6").find("option:selected").text();
                        }
                    }
                }
            }
        }
    }
    if ($("#showimg").attr("src")) {
        imgpath = $("#showimg").attr("src");
        imgpath = imgpath.substring(imgpath.lastIndexOf("/")+1);
    }
    else {
        imgpath = "";
    }

    if ($("#p2").attr("checked") == 1) {
        if ($("#sheng").val() != 0) {
            sheng = $("#sheng").val();
        }

        if ($("#shi").val() != 0) {
            shi = $("#shi").val();
        }
    }

    title = $("#title").val();
    owner = $("#ower").val();
    pricem = 0;
    pricex = $("#pricemax").val();
    count = $("#goodscount").val();
    infomation = escape(KE.html("info"));
    infortext = KE.text("info");
    pname = $("#name").val();
    /****************提交数据准备END**************/
    $.ajax({
        type: "post",
        url: "/AjaxCommand/InsertBuy.ashx",
        data: { type: "insert",
            Gcid: cid,
            Gpcid: pcid,
            Gtitle: title,
            Gowner: owner,
            Gpricem: pricem,
            Gpricex: pricex,
            Gdatetime: overtime,
            Gcount: count,
            Gimgpath: imgpath,
            Gsheng: sheng,
            Gshi: shi,
            Ginfomation: infomation,
            Ginfortext: infortext,
            Gname: pname,
            Gcidtree: cidtree,
            GcidtextTree: cidtextTree,
            Gkeyprop: keyprop
        },
        beforeSend: function (XMLHttpRequest) {
            asyncbox.open({
                id: 'waitdialog',
                title: '',
                width: 330,
                height: 115,
                html: "<div class='dialogcss'><p><img src='images/bigload.gif' style='vertical-align:middle;' /><label class='loadcss'><strong>&nbsp正在提交，请稍等...</strong></label></p></div>"
            });
            $(".asyncbox_close").remove();
        },
        success: function (data, textStatus, jqXHR) {
            $.close("waitdialog");
            if (data == "ok") {
                $.success("提交成功", "提交成功", function (action) {
                    //location.reload();
                    location.href = "/buysuccess.html";
                });
            }
            else {
                $.error("您输入的描述中包含[" + data + "]敏感字符，请检查后再提交！", "用词敏感");
            }
        },
        complete: function (XMLHttpRequest, textStatus) {
            $.close("waitdialog");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // 通常 textStatus 和 errorThrown 之中
            // 只有一个会包含信息
            //alert(XMLHttpRequest); // 调用本次AJAX请求时传递的options参数
            alert("提交失败，请稍后再试！");
        }
    });
}
function checkForm() {
    var result = false;

    if ($("#title").val() != "" && $("#goodscount").val() != "" && $("#pricemax").val() != "" && $("#name").val() != "") {
        result = true;
    }
    return result;
}

function showerrdialog(objstring) {
    asyncbox.tips(objstring, 'error');
    $("#asynctips_error_content").css("font", "14px 微软雅黑, 'Microsoft YaHei', Helvetica, Tahoma, StSun, 宋体, SimSun, sans-serif !important;");
}
function showloaddialog() {
    asyncbox.open({
        id: 'getcatlist',
        title: '',
        width: 330,
        height: 115,
        html: "<div class='dialogcss'><p><img src='/images/bigload.gif' style='vertical-align:middle;' /><label class='loadcss'><strong>&nbsp加载中...</strong></label></p></div>"
    });
    $(".asyncbox_close").remove();
}

function getselectCit(inx) {
    var selindex = inx - 1;
    var sc = "";
    if (inx == 1) {
        $("#selectcit").empty();
        sc = "<span>" + $("#cit" + inx).find("option:selected").text() + "</span>";
        $("#selectcit").append(sc);
    }
    else {
        $("#selectcit span").eq(selindex-1).nextAll().remove();
        sc = "<span><font color='#A91305'>&nbsp>&nbsp</font>" + $("#cit" + inx).find("option:selected").text() + "</span>";
        $("#selectcit").append(sc);
    }    
}